﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace DTC
{
    public partial class FrmLogin : UIBase
    {
        public FrmLogin()
        {
            
            InitializeComponent();
            
        }
        FrmDisplay dis;
        FrmSetting frmSet;
        public Common.FIO01 m_io;

        public string CheckWelcomeInputValid()
        {
            string valid = "OK";
            if (this.textBox1.Text == string.Empty)
            {
                valid = "生产工单为空";
            }
            else if (this.comboBox1.Text == string.Empty)
            {
                valid = "产品型号为空";
            }
            else if (this.textBox2.Text == string.Empty)
            {
                valid = "操作员为空";
            }
            return valid;

        }
        private void StartTestPage()
        {
            string valid = CheckWelcomeInputValid();
            if (valid != "OK")
            {
                if (ShowQuestion(valid + "，是否继续？") != DialogResult.Yes)
                    return;
            }
            if (dis == null)
            {
                this.Visible = false;
                dis = new FrmDisplay();
                dis.ModelName = this.comboBox1.Text;
                dis.Show();
            }

        }
        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (ShowQuestion("真的要退出吗？") == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        private void btnSetting_Click(object sender, EventArgs e)
        {
            if (frmSet == null)
            {
                frmSet = new FrmSetting();
                frmSet.ShowDialog();
            }
            else
            {
                frmSet.ShowDialog();
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            UIData_Load();
            
        }

        private void UIData_Load()
        {
            comboBox1.Text = ConfigurationManager.AppSettings["CurrentModel"];
            textBox1.Text = ConfigurationManager.AppSettings["MO"];
            textBox2.Text = ConfigurationManager.AppSettings["Operator"];
            string ModelList = ConfigurationManager.AppSettings["ModelList"];
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(ModelList.Split(new char[] { ',' }));
        }
        private void buttonX1_Click(object sender, EventArgs e)
        {
            UIData_Save();
            if (dis != null)
            {
            }
            else
            {
                this.StartTestPage();
            }
        }

        private void UIData_Save()
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection cfa = configuration.AppSettings;
            //修改节点值
            cfa.Settings["CurrentModel"].Value = this.comboBox1.Text;
            cfa.Settings["MO"].Value = this.textBox1.Text;
            cfa.Settings["Operator"].Value = this.textBox2.Text;
            if (!comboBox1.Items.Contains(comboBox1.Text))
            {
                cfa.Settings["ModelList"].Value += "," + comboBox1.Text;
            }
            //最后保存修改的节点
            configuration.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }

    }
}
